//
//  QLFavoriteManager.m
//  WeiMiLan
//
//  Created by anan on 14/12/17.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "QLFavoriteManager.h"

@implementation QLFavoriteManager

-(id) init
{
    self = [super init];
    if (self) {
        self.favorites = [[NSMutableArray alloc] init];
        self.favoriteDic =[[NSMutableDictionary alloc] init];
    }
    return self;
}

static QLFavoriteManager *instance;

+ (QLFavoriteManager*)shareInstance
{
    if (instance == nil) {
        instance = [[QLFavoriteManager alloc] init];
    }
    return instance;
}

- (void)addFavoriteWithUserId:(int)userId productId:(int)productId callback:(QLHttpResultBlock)callback
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic ql_setInterge:userId forKey:@"userId"];
    [dic ql_setInterge:productId forKey:@"ID"];
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [requestManager GET:@"http://ping.vsmu.cn/app/Interface/Package?type=1004" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self loadAllWithUserId:userId callback:^(BOOL isCache, id data) {
            [SVProgressHUD showSuccessWithStatus:@"收藏添加成功"];
            if (callback) {
                callback(NO,self.favorites);
            }
        }];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showSuccessWithStatus:@"收藏添加失败"];
        if (callback) {
            callback(NO,nil);
        }
    }];
}

- (BOOL)isFavoriteWithUserId:(int)userId productId:(int)productId
{
    NSString *key = [NSString stringWithFormat:@"%d_%d",userId,productId];
    return [self.favoriteDic ql_hasObjectForKey:key];
}

- (void)deleteFavoriteWithUserId:(int)userId productId:(int)productId callback:(QLHttpResultBlock)callback
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic ql_setInterge:userId forKey:@"userId"];
    [dic ql_setInterge:productId forKey:@"ID"];
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [requestManager GET:@"http://ping.vsmu.cn/app/Interface/Package?type=1005" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [SVProgressHUD showSuccessWithStatus:@"收藏删除成功"];
        NSString *key = [NSString stringWithFormat:@"%d_%d",userId,productId];
        [self.favoriteDic removeObjectForKey:key];
        if (callback) {
            callback(NO,self.favorites);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showSuccessWithStatus:@"收藏删除失败"];
        if (callback) {
            callback(NO,nil);
        }
    }];
}

- (void)loadAllWithUserId:(int)userId callback:(QLHttpResultBlock)callback
{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic ql_setInterge:userId forKey:@"userId"];
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [requestManager GET:@"http://ping.vsmu.cn/app/Interface/Package?ORDER_DIRECTION=0&type=1003" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self setFavorites:responseObject[@"PACKAGE_RSP"][@"PACKAGE"] myUserId:userId];
        if (callback) {
            callback(NO,self.favorites);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (callback) {
            callback(NO,nil);
        }
    }];
}

- (void)setFavorites:(NSMutableArray *)favorites myUserId:(int)myUserId
{
    _favorites = favorites;
    [self.favoriteDic removeAllObjects];
    if (favorites) {
        for (NSDictionary *dic in favorites) {
            NSString *key = [NSString stringWithFormat:@"%d_%d",myUserId,[dic ql_intForKey:@"ID"]];
            [self.favoriteDic setObject:dic forKey:key];
        }
    }
}

@end
